package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BillOriginal;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.rOutappMain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lenovo on 2016/6/1.
 */
public class SingleBillListFragment extends BaseListFragment {
  private BaseListAdapter mAdapter;

  //    @Override
  //    public void onViewCreated(View view, Bundle savedInstanceState) {
  //        // 执行完父类方法才存在mListView.
  //        super.onViewCreated(view, savedInstanceState);
  //        mListview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  //        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  //            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  //                mAdapter.notifyDataSetChanged();
  //
  //            }
  //        });
  //    }

  @Override public String getUrl() {
    return ApiConst.URL_BILL_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    String memberCode = CurrentUser.newInstance(getActivity()).getMemberCode();
    params.put("curPage", "1");
    params.put("pageSize", "10000");
    params.put("memberCode", memberCode);
    params.put("minkindCode", memberCode + "-DJLB-IN-CGDDRK");
    params.put("moutkindCode", memberCode + "-DJLB-OUT-XSDDCK");
    params.put("minbillStatusCode", "");
    params.put("moutbillStatus", "");
    params.put("customerId", "");
    params.put("ifAllpay", "F");
    params.put("ifInvoiceFinished", "F");
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("type", "mobile");
    return params;
  }

  @Override public void resolveResponse(String arg0) {

    BillOriginal attendanceListOriginal = new Gson().fromJson(arg0, BillOriginal.class);

    List<rOutappMain> rows = attendanceListOriginal.getRows();
    setAdapter(rows);
  }

  private void setAdapter(List<rOutappMain> detail) {
    if (detail == null) {
      detail = new ArrayList<rOutappMain>();
    }

    if (mAdapter == null) {
      mAdapter = new BaseListAdapter<rOutappMain>(getActivity(), detail) {

        @Override
        public void bindData(int position, BaseListAdapter.ViewHolder vh, rOutappMain item) {
          vh.mTitleOneTv.setText(getContext().getString(R.string.inOutStoreNo));
          vh.mContentOneTv.setText(item.getMoutbillNo());
          vh.mTitleTwoTv.setText(getContext().getString(R.string.customerName));
          vh.mContentTwoTv.setText(item.getMcustomerName());
          vh.mTitleThreeTv.setText(getContext().getString(R.string.salesManName));
          vh.mContentThreeTv.setText(item.getMoutappmanName());
        }
      };
      setListAdapter(mAdapter);
      return;
    }

    mAdapter.clear();
    mAdapter.addAll(detail);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    rOutappMain item = (rOutappMain) mAdapter.getItem(position - 1);
    CustomerDialog content =
        new CustomerDialog(item.getMoutbillNo(), item.getId(), item.getRegStaffId(),
            item.getSumPrice() + "");
    Intent intent = new Intent();
    intent.putExtra(ContractChoosePagerActivity.EXTRA_CONTRACT, content);
    intent.putExtra(ContractChoosePagerActivity.EXTRA_TYPE, ContractChoosePagerActivity.ORDER);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
  }

  public void refresh() {
    volleyPost();
  }

  //    @Override
  //    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  //        inflater.inflate(R.menu.menu_confirm, menu);
  //
  //    }
  //
  //    @Override
  //    public boolean onOptionsItemSelected(MenuItem item) {
  //        switch (item.getItemId()) {
  //
  //            case R.id.menu_item_confirm:
  //                SparseBooleanArray checkedItems = mListview.getCheckedItemPositions();
  //                if (checkedItems != null) {
  //                    //选择后要清空之前的数据.用新建对象的方式清空.
  //                    RProInvoiceRecd receiptDetailUpdate=new RProInvoiceRecd();
  //                    receiptDetailUpdate.setOrderType("out");
  //                    ArrayList<String>  ids=new ArrayList<String>();
  //
  //                    ArrayList<String>  names=new ArrayList<String>();
  //                    double amount=0.0;//订单金额.
  //                    double unReceiptAmount=0.0;//未开票金额.
  //
  //
  //
  //
  //                    for (int i = 0; i < checkedItems.size(); i++) {
  //                        //获取选中数据.
  //                        if (checkedItems.valueAt(i)) {
  //                            rOutappMain select = (rOutappMain) mAdapter.getItem(checkedItems.keyAt(i)-1);
  //                            names.add(select.getMoutbillNo());
  //                            ids.add(select.getId());
  //                            amount+= select.getSumPrice();
  //                            unReceiptAmount+= select.getResidualInvoiceAmount();
  //
  //
  //                        }
  //                    }
  //                    receiptDetailUpdate.setOrderId(MyUtils.getJoinString(ids));
  //                    receiptDetailUpdate.setOrderName(MyUtils.getJoinString(names));
  //                    receiptDetailUpdate.setContractAmount(amount);
  //                    receiptDetailUpdate.setMakedInvoiceAmount(unReceiptAmount);
  //                    Intent i=new Intent();
  //                    i.putExtra(ContractPagerActivity.EXTRA_UPDATE_CONTRACT, receiptDetailUpdate);
  //                    getActivity().setResult(Activity.RESULT_OK, i);
  //                    getActivity().finish();
  //                }
  //
  //                return true;
  //
  //            default:
  //                return super.onOptionsItemSelected(item);
  //        }
  //    }
}
